home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / lib / calc / help / libcalc < prev    next >
Text File  |  1995-07-17  |  14KB  |  353 lines

  1.     USING THE ARBITRARY PRECISION ROUTINES IN A C PROGRAM
  2.  
  3.  
  4. Part of the calc release consists of an arbitrary precision math library.
  5. This library is used by the calc program to perform its own calculations.
  6. If you wish, you can ignore the calc program entirely and call the arbitrary
  7. precision math routines from your own C programs.
  8.  
  9. The library is called libmath.a, and provides routines to handle arbitrary
  10. precision arithmetic with integers, rational numbers, or complex numbers.
  11. There are also many numeric functions such as factorial and gcd, along
  12. with some transcendental functions such as sin and exp.
  13.  
  14. -------------
  15. INCLUDE FILES
  16. -------------
  17.  
  18. To use any of these routines in your own programs, you need to include the
  19. appropriate include file.  These include files are:
  20.  
  21.     zmath.h        (for integer arithmetic)
  22.     qmath.h        (for rational arithmetic)
  23.     cmath.h        (for complex number arithmetic)
  24.  
  25. You never need to include more than one of the above files, even if you wish
  26. to use more than one type of arithmetic, since qmath.h automatically includes
  27. zmath.h, and cmath.h automatically includes qmath.h.
  28.  
  29. The prototypes for the available routines are listed in the above include
  30. files.  Some of these routines are meant for internal use, and so aren't
  31. convenient for outside use.  So you should read the source for a routine
  32. to see if it really does what you think it does.  I won't guarantee that
  33. obscure internal routines won't change or disappear in future releases!
  34.  
  35. When calc is installed, all of the include files needed to build
  36. libcalc.a along with the library itself (and the lint library
  37. llib-lcalc.ln, if made) are installed into /usr/skunk/lib/calc.
  38.  
  39. External programgs may want to compile with:
  40.     
  41.     -I/usr/skunk/lib/calc -L/usr/skunk/lib/calc -lcalc
  42.  
  43. --------------
  44. ERROR HANDLING
  45. --------------
  46.  
  47. You program MUST provide a function called math_error.  This is called by
  48. the math routines on an error condition, such as malloc failures or a
  49. division by zero.  The routine is called in the manner of printf, with a
  50. format string and optional arguments.  (However, none of the low level math
  51. routines currently uses formatting, so if you are lazy you can simply use
  52. the first argument as a simple error string.)  For example, one of the
  53. error calls you might expect to receive is:
  54.  
  55.     math_error("Division by zero");
  56.  
  57. Your program can handle errors in basically one of two ways.  Firstly, it
  58. can simply print the error message and then exit.  Secondly, you can make
  59. use of setjmp and longjmp in your program.  Use setjmp at some appropriate
  60. level in your program, and use longjmp in the math_error routine to return
  61. to that level and so recover from the error.  This is what the calc program
  62. does.
  63.  
  64. ---------------
  65. OUTPUT ROUTINES
  66. ---------------
  67.  
  68. The output from the routines in the library normally goes to stdout.  You
  69. can divert that output to either another FILE handle, or else to a string.
  70. Read the routines in zio.c to see what is available.  Diversions can be
  71. nested.
  72.  
  73. You use math_setfp to divert output to another FILE handle.  Calling
  74. math_setfp with stdout restores output to stdout.
  75.  
  76. Use math_divertio to begin diverting output into a string.  Calling
  77. math_getdivertedio will then return a string containing the output, and
  78. clears the diversion.  The string is reallocated as necessary, but since
  79. it is in memory, there are obviously limits on the amount of data that can
  80. be diverted into it.  The string needs freeing when you are done with it.
  81.  
  82. Calling math_cleardiversions will clear all the diversions to strings, and
  83. is useful on an error condition to restore output to a known state.  You
  84. should also call math_setfp on errors if you had changed that.
  85.  
  86. If you wish to mix your own output with numeric output from the math routines,
  87. then you can call math_chr, math_str, math_fill, math_fmt, or math_flush.
  88. These routines output single characters, output null-terminated strings,
  89. output strings with space filling, output formatted strings like printf, and
  90. flush the output.  Output from these routines is diverted as described above.
  91.  
  92. You can change the default output mode by calling math_setmode, and you can
  93. change the default number of digits printed by calling math_setdigits.  These
  94. routines return the previous values.  The possible modes are described in
  95. zmath.h.
  96.  
  97. --------------
  98. USING INTEGERS
  99. --------------
  100.  
  101. The arbitrary precision integer routines define a structure called a ZVALUE.
  102. This is defined in zmath.h.  A ZVALUE contains a pointer to an array of
  103. integers, the length of the array, and a sign flag.  The array is allocated
  104. using malloc, so you need to free this array when you are done with a
  105. ZVALUE.  To do this, you should call zfree with the ZVALUE as an argument
  106. (or call freeh with the pointer as an argument) and never try to free the
  107. array yourself using free.  The reason for this is that sometimes the pointer
  108. points to one of two statically allocated arrays which should NOT be freed.
  109.  
  110. The ZVALUE structures are passed to routines by value, and are returned
  111. through pointers.  For example, to multiply two small integers together,
  112. you could do the following:
  113.  
  114.     ZVALUE    z1, z2, z3;
  115.  
  116.     itoz(3L, &z1);
  117.     itoz(4L, &z2);
  118.     zmul(z1, z2, &z3);
  119.  
  120. Use zcopy to copy one ZVALUE to another.  There is no sharing of arrays
  121. between different ZVALUEs even if they have the same value, so you MUST
  122. use this routine.  Simply assigning one value into another will cause
  123. problems when one of the copies is freed.  However, the special ZVALUE
  124. values _zero_ and _one_ CAN be assigned to variables directly, since their
  125. values of 0 and 1 are so common that special checks are made for them.
  126.  
  127. For initial values besides 0 or 1, you need to call itoz to convert a long
  128. value into a ZVALUE, as shown in the above example.  Or alternatively,
  129. for larger numbers you can use the atoz routine to convert a string which
  130. represents a number into a ZVALUE.  The string can be in decimal, octal,
  131. hex, or binary according to the leading digits.
  132.  
  133. Always make sure you free a ZVALUE when you are done with it or when you
  134. are about to overwrite an old ZVALUE with another value by passing its
  135. address to a routine as a destination value, otherwise memory will be
  136. lost.  The following shows an example of the correct way to free memory
  137. over a long sequence of operations.
  138.  
  139.     ZVALUE z1, z2, z3;
  140.  
  141.     z1 = _one_;
  142.     atoz("12345678987654321", &z2);
  143.     zadd(z1, z2, &z3);
  144.     zfree(z1);
  145.     zfree(z2);
  146.     zsquare(z3, &z1);
  147.     zfree(z3);
  148.     itoz(17L, &z2);
  149.     zsub(z1, z2, &z3);
  150.     zfree(z1);
  151.     zfree(z2);
  152.     zfree(z3);
  153.  
  154. There are some quick checks you can make on integers.  For example, whether
  155. or not they are zero, negative, even, and so on.  These are all macros
  156. defined in zmath.h, and should be used instead of checking the parts of the
  157. ZVALUE yourself.  Examples of such checks are:
  158.  
  159.     ziseven(z)    (number is even)
  160.     zisodd(z)    (number is odd)
  161.     ziszero(z)    (number is zero)
  162.     zisneg(z)    (number is negative)
  163.     zispos(z)    (number is positive)
  164.     zisunit(z)    (number is 1 or -1)
  165.     zisone(z)    (number is 1)
  166.  
  167. There are two types of comparisons you can make on ZVALUEs.  This is whether
  168. or not they are equal, or the ordering on size of the numbers.  The zcmp
  169. function tests whether two ZVALUEs are equal, returning TRUE if they differ.
  170. The zrel function tests the relative sizes of two ZVALUEs, returning -1 if
  171. the first one is smaller, 0 if they are the same, and 1 if the first one
  172. is larger.
  173.  
  174. ---------------
  175. USING FRACTIONS
  176. ---------------
  177.  
  178. The arbitrary precision fractional routines define a structure called NUMBER.
  179. This is defined in qmath.h.  A NUMBER contains two ZVALUEs for the numerator
  180. and denominator of a fraction, and a count of the number of uses there are
  181. for this NUMBER.  The numerator and denominator are always in lowest terms,
  182. and the sign of the number is contained in the numerator.  The denominator
  183. is always positive.  If the NUMBER is an integer, the denominator has the
  184. value 1.
  185.  
  186. Unlike ZVALUEs, NUMBERs are passed using pointers, and pointers to them are
  187. returned by functions.  So the basic type for using fractions is not really
  188. (NUMBER), but is (NUMBER *).  NUMBERs are allocated using the qalloc routine.
  189. This returns a pointer to a number which has the value 1.  Because of the
  190. special property of a ZVALUE of 1, the numerator and denominator of this
  191. returned value can simply be overwritten with new ZVALUEs without needing
  192. to free them first.  The following illustrates this:
  193.  
  194.     NUMBER *q;
  195.  
  196.     q = qalloc();
  197.     itoz(55L, &q->num);
  198.  
  199. A better way to create NUMBERs with particular values is to use the itoq,
  200. iitoq, or atoq functions.  Using itoq makes a long value into a NUMBER,
  201. using iitoq makes a pair of longs into the numerator and denominator of a
  202. NUMBER (reducing them first if needed), and atoq converts a string representing
  203. a number into the corresponding NUMBER.  The atoq function accepts input in
  204. integral, fractional, real, or exponential formats.  Examples of allocating
  205. numbers are:
  206.  
  207.     NUMBER *q1, *q2, *q3;
  208.  
  209.     q1 = itoq(66L);
  210.     q2 = iitoq(2L, 3L);
  211.     q3 = atoq("456.78");
  212.  
  213. Also unlike ZVALUEs, NUMBERs are quickly copied.  This is because they contain
  214. a link count, which is the number of pointers there are to the NUMBER.  The
  215. qlink macro is used to copy a pointer to a NUMBER, and simply increments
  216. the link count and returns the same pointer.  Since it is a macro, the
  217. argument should not be a function call, but a real pointer variable.  The
  218. qcopy routine will actually make a new copy of a NUMBER, with a new link
  219. count of 1.  This is not usually needed.
  220.  
  221. NUMBERs are deleted using the qfree routine.  This decrements the link count
  222. in the NUMBER, and if it reaches zero, then it will deallocate both of
  223. the ZVALUEs contained within the NUMBER, and then puts the NUMBER structure
  224. onto a free list for quick reuse.  The following is an example of allocating
  225. NUMBERs, copying them, adding them, and finally deleting them again.
  226.  
  227.     NUMBER *q1, *q2, *q3;
  228.  
  229.     q1 = itoq(111L);
  230.     q2 = qlink(q1);
  231.     q3 = qadd(q1, q2);
  232.     qfree(q1);
  233.     qfree(q2);
  234.     qfree(q3);
  235.  
  236. Because of the passing of pointers and the ability to copy numbers easily,
  237. you might wish to use the rational number routines even for integral
  238. calculations.  They might be slightly slower than the raw integral routines,
  239. but are more convenient to program with.
  240.  
  241. The prototypes for the fractional routines are defined in qmath.h.
  242. Many of the definitions for integer functions parallel the ones defined
  243. in zmath.h.  But there are also functions used only for fractions.
  244. Examples of these are qnum to return the numerator, qden to return the
  245. denominator, qint to return the integer part of, qfrac to return the
  246. fractional part of, and qinv to invert a fraction.
  247.  
  248. There are some transcendental functions in the library, such as sin and cos.
  249. These cannot be evaluated exactly as fractions.  Therefore, they accept
  250. another argument which tells how accurate you want the result.  This is an
  251. "epsilon" value, and the returned value will be within that quantity of
  252. the correct value.  This is usually an absolute difference, but for some
  253. functions (such as exp), this is a relative difference.  For example, to
  254. calculate sin(0.5) to 100 decimal places, you could do:
  255.  
  256.     NUMBER *q, *ans, *epsilon;
  257.  
  258.     q = atoq("0.5");
  259.     epsilon = atoq("1e-100");
  260.     ans = qsin(q, epsilon);
  261.  
  262. There are many convenience macros similar to the ones for ZVALUEs which can
  263. give quick information about NUMBERs.  In addition, there are some new ones
  264. applicable to fractions.  These are all defined in qmath.h.  Some of these
  265. macros are:
  266.  
  267.     qiszero(q)    (number is zero)
  268.     qisneg(q)    (number is negative)
  269.     qispos(q)    (number is positive)
  270.     qisint(q)    (number is an integer)
  271.     qisfrac(q)    (number is fractional)
  272.     qisunit(q)    (number is 1 or -1)
  273.     qisone(q)    (number is 1)
  274.  
  275. The comparisons for NUMBERs are similar to the ones for ZVALUEs.  You use the
  276. qcmp and qrel functions.
  277.  
  278. There are four predefined values for fractions.  You should qlink them when
  279. you want to use them.  These are _qzero_, _qone_, _qnegone_, and _qonehalf_.
  280. These have the values 0, 1, -1, and 1/2.  An example of using them is:
  281.  
  282.     NUMBER *q1, *q2;
  283.  
  284.     q1 = qlink(&_qonehalf_);
  285.     q2 = qlink(&_qone_);
  286.  
  287. ---------------------
  288. USING COMPLEX NUMBERS
  289. ---------------------
  290.  
  291. The arbitrary precision complex arithmetic routines define a structure
  292. called COMPLEX.  This is defined in cmath.h.  This contains two NUMBERs
  293. for the real and imaginary parts of a complex number, and a count of the
  294. number of links there are to this COMPLEX number.
  295.  
  296. The complex number routines work similarly to the fractional routines.
  297. You can allocate a COMPLEX structure using comalloc (NOT calloc!).
  298. You can construct a COMPLEX number with desired real and imaginary
  299. fractional parts using qqtoc.  You can copy COMPLEX values using clink
  300. which increments the link count.  And you free a COMPLEX value using cfree.
  301. The following example illustrates this:
  302.  
  303.     NUMBER *q1, *q2;
  304.     COMPLEX *c1, *c2, *c3;
  305.  
  306.     q1 = itoq(3L);
  307.     q2 = itoq(4L);
  308.     c1 = qqtoc(q1, q2);
  309.     qfree(q1);
  310.     qfree(q2);
  311.     c2 = clink(c1);
  312.     c3 = cmul(c1, c2);
  313.     cfree(c1);
  314.     cfree(c2);
  315.     cfree(c3);
  316.  
  317. As a shortcut, when you want to manipulate a COMPLEX value by a real value,
  318. you can use the caddq, csubq, cmulq, and cdivq routines.  These accept one
  319. COMPLEX value and one NUMBER value, and produce a COMPLEX value.
  320.  
  321. There is no direct routine to convert a string value into a COMPLEX value.
  322. But you can do this yourself by converting two strings into two NUMBERS,
  323. and then using the qqtoc routine.
  324.  
  325. COMPLEX values are always returned from these routines.  To split out the
  326. real and imaginary parts into normal NUMBERs, you can simply qlink the
  327. two components, as shown in the following example:
  328.  
  329.     COMPLEX *c;
  330.     NUMBER *rp, *ip;
  331.  
  332.     c = calloc();
  333.     rp = qlink(c->real);
  334.     ip = qlink(c->imag);
  335.  
  336. There are many macros for checking quick things about complex numbers,
  337. similar to the ZVALUE and NUMBER macros.  In addition, there are some
  338. only used for complex numbers.  Examples of macros are:
  339.  
  340.     cisreal(c)    (number is real)
  341.     cisimag(c)    (number is pure imaginary)
  342.     ciszero(c)    (number is zero)
  343.     cisrunit(c)    (number is 1 or -1)
  344.     cisiunit(c)    (number is i or -i)
  345.     cisunit(c)    (number is 1, -1, i, or -i)
  346.  
  347. There is only one comparison you can make for COMPLEX values, and that is
  348. for equality.  The ccmp function returns TRUE if two complex numbers differ.
  349.  
  350. There are three predefined values for complex numbers.  You should clink
  351. them when you want to use them.  They are _czero_, _cone_, and _conei_.
  352. These have the values 0, 1, and i.
  353.